package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProductDevelopCountAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ProductSalesOriginal;
import com.isunland.managesystem.entity.ProductSalesSearchContent;
import com.isunland.managesystem.entity.ProductSearchContent;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by lenovo on 2016/6/15.
 * 产品开发统计。
 */
public class ProductDevelopCountFragment extends BaseListFragment {
  protected static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.ProductDevelopCountFragment.EXTRA_VALUE";
  private static final int COUNT = 0;
  private ArrayList<ProductSalesOriginal.ProductSalesContent> mList;
  private ProductSalesSearchContent content;//登记列表传的查询条件
  private ProductSearchContent countContent =
      new ProductSearchContent("s.in_month", "s.marketend", "s.customer", "s.REG_STAFF_NAME",
          "s.product_kind", "s.product");//统计列表的统计项
  private String gropbystr =
      "s.in_month,s.marketend,s.customer,s.REG_STAFF_NAME,s.product_kind,s.product";
  private CurrentUser mUser;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar()
        .setTitle(R.string.productDevelopCountList);
    content = (ProductSalesSearchContent) getArguments().getSerializable(EXTRA_VALUE);
    mUser = CurrentUser.newInstance(getActivity());
    super.onCreate(savedInstanceState);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mListview.setDivider(null);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }

        break;
      case R.id.menu_item_query:
        //                Intent intent=new Intent(getActivity(), ProductCountActivity.class);
        //                intent.putExtra(ProductCountFragment.EXTRA_COUNT,countContent);
        //                startActivityForResult(intent,COUNT);
        Intent intent = new Intent(getActivity(), ProductDevelopCountSearchActivity.class);
        intent.putExtra(ProductDevelopCountSearchFragment.EXTRA_COUNT, countContent);
        startActivityForResult(intent, COUNT);
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {
    return ApiConst.ULR_PRODUCTDEVELOP_COUNT;
  }

  @Override public HashMap<String, String> getparameters() {

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("beginInMonth", content.getStartDate());
    params.put("endInMonth", content.getEndDate());
    params.put("customer", content.getCustomerName());
    params.put("salesManName", content.getSalePersonName());
    params.put("product", content.getProductName());
    params.put("memberCode", mUser.getMemberCode());
    params.put("gropbystr", gropbystr);//统计项，中间用逗号隔开

    return params;
  }

  @Override public void resolveResponse(String json) {
    ProductSalesOriginal original = new Gson().fromJson(json, ProductSalesOriginal.class);
    ArrayList<ProductSalesOriginal.ProductSalesContent> rows =
        (ArrayList<ProductSalesOriginal.ProductSalesContent>) original.getRows();
    if (mList == null) {
      mList = new ArrayList<ProductSalesOriginal.ProductSalesContent>();
    }
    mList.clear();
    mList.addAll(rows);
    setAdapter(mList);
  }

  private void setAdapter(ArrayList<ProductSalesOriginal.ProductSalesContent> mList) {

    ProductDevelopCountAdapter mAdapter =
        new ProductDevelopCountAdapter(getActivity(), mList, countContent);

    setListAdapter(mAdapter);
    ((ProductDevelopCountAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == COUNT) {
      countContent = (ProductSearchContent) data.getSerializableExtra(
          ProductDevelopCountSearchFragment.EXTRA_COUNT);
      gropbystr = updateParams(countContent);
      LogUtil.i("gropbystr===" + gropbystr);
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  private String updateParams(ProductSearchContent countContent) {
    StringBuilder str = new StringBuilder();
    if (!TextUtils.isEmpty(countContent.getMonth())) {
      str.append(countContent.getMonth()).append(",");
    }
    if (!TextUtils.isEmpty(countContent.getMarket())) {
      str.append(countContent.getMarket()).append(",");
    }
    if (!TextUtils.isEmpty(countContent.getCustomer())) {
      str.append(countContent.getCustomer()).append(",");
    }
    if (!TextUtils.isEmpty(countContent.getSaleman())) {
      str.append(countContent.getSaleman()).append(",");
    }
    if (!TextUtils.isEmpty(countContent.getProductType())) {
      str.append(countContent.getProductType()).append(",");
    }
    if (!TextUtils.isEmpty(countContent.getProduct())) {
      str.append(countContent.getProduct());
    }
    return str.toString();
  }

  protected static ProductDevelopCountFragment newInstence(ProductSalesSearchContent content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VALUE, content);
    ProductDevelopCountFragment mfragment = new ProductDevelopCountFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}

